;(function(){
var role = require("./role");
var BusCon = require("../../core/bus/_con");
var UserCon = require("../../core/user/_con");
var GoodsCon = require("../../core/goods/_con");
var AchCon = require("../../core/ach/_con");
var db = require("../../core/db/index");
var config = require("../../core/_include/config");
var ResponObj = require("../_include/responObj");
var UserAddressCon = require("../../core/user_address/_con");
module.exports = {
	prefix:"/user/address",
	get:{
		//获取当前用户的地址信息(ID)列表
		"/":function (){
			var user  = role.getUser(this);
			var query = this.request.query;
			var num = (+query.num)||5;
			var userAddresses = user.getAddress(num,query.page);
			this.body = ResponObj("json",userAddresses);
		},
		//获取当前用户的地址详细信息列表
		"/detail":function (){
			var user  = role.getUser(this);
			var query = this.request.query;
			var num = (+query.num)||5;
			var userAddresses = user.getAddress(num,query.page);
			userAddresses.forEach(function(ua_id,index){
				userAddresses[index] = UserAddressCon.get(ua_id);
			});
			this.body = ResponObj("json",userAddresses);
		},
		//获取默认收获地址的详细信息
		"/default":function (){
			var user  = role.getUser(this);
			this.body = ResponObj("json",user.default_ua_id&&UserAddressCon.get(user.default_ua_id));
		},
		//获取三级城市列表zfj@2014.10.24
		"/city/:parent_id":function(){
			var parent_id = this.params.parent_id;
			var arr = new Array();
			arr = parent_id.split(":");
			var pid = arr[1];
			var list = db.find_list(config.dbname.city,{"parent_id":pid});
			this.body = ResponObj("json",list);
			//console.log(pid);
		}
	},
	post:{
		//为当前用户 添加 收获地址
		"/":function (){
			var user  = role.getUser(this);
			//无需判断重复，用户自行删除
			var ua_id = user.addAddress(this.request.body);
			this.body = ResponObj("json",UserAddressCon.get(ua_id));
		}
	},
	"delete":{
		"/":function (){
			var user  = role.getUser(this);
			var address_id = this.request.body.address_id;
			user.removeAddress(address_id)
			//返回完整列表
			module.exports.get["/detail"].call(this);
		}
	},
	put:{
		"/default":function (){
			var user  = role.getUser(this);
			var default_ua_id = this.request.body.address_id;
			var address_id = user.setDefaultAddress(default_ua_id);
			this.body = ResponObj("json",default_ua_id);
		}
	}
}	
}());